<template>
  <div class="wsContainerBox">
    <!-- {{ roomCount }} -- {{ connectStatus }} -->
    <!-- 未连接，断开连接 -->
    <!-- [0, 2].includes(connectStatus) ||  -->
    <div v-if="roomCount == 1">
      <div class="title">
        接入手机
        <img :src="closeIcon" class="close-style" @click="handlePopoverClose"
          @touchstart.prevent="handlePopoverClose" />
      </div>
      <vueQr v-if="ws.wsUrl" :logoSrc="logo" :text="ws.wsUrl" :size="200" />
      <div class="subTitle">使用<span>数字教材APP</span>扫码接入手机 实现“移动+PC”联动授课</div>
    </div>
    <!-- 连接成功 -->
    <!-- {{ connectStatus }} -->
    <div v-else>
      <div class="title">
        接入手机
        <img :src="closeIcon" class="close-style" @click="handlePopoverClose"
          @touchstart.prevent="handlePopoverClose" />
      </div>
      <!-- 连接成功 -->
      <div v-if="connectStatus == 1">
        <div class="connectStatus">
          <img :src="successOk" alt="" />
        </div>
        <div class="footerBox">
          <span class="equre"> 连接码{{ ws.linkCode }} </span>
          <span class="connectStatusDot">
            <span class="dot"></span>
            已连接</span>
          <span @click="overConnect" class="closeConnect">断开连接</span>
        </div>
      </div>
      <!-- 连接失败 -->
      <div v-if="connectStatus == 2">
        <div class="connectStatus">
          <img :src="error" alt="" />
        </div>
        <div class="footerBox">
          <span class="connectStatusDot">
            <span class="dot"></span>
            已连接</span>
          <span @click="redoLink" class="closeConnect">重新连接</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
// vue3.0 (support vite)
import vueQr from 'vue-qr/src/packages/vue-qr.vue';
import useStore from '../../../store';
import logo from '../../../assets/logo.png';

import error from '../../../assets/connectErr.png';
import success from '../../../assets/connectSuccess.png';
import successOk from '../../../assets/successOk.svg';

import closeIcon from '../../../assets/teaching/close.svg';
import { nextTick, onMounted, ref } from 'vue';
import { storeToRefs } from 'pinia';

const store = useStore();
const { ws } = store;

const { roomCount, connectStatus } = storeToRefs(ws);

const emits = defineEmits(['close']);

const handlePopoverClose = () => {
  emits('close');
};

// 断开连接
const overConnect = () => {
  connectStatus.value = 3;
  if (ws.wsref) {
    ws.close();
  }
};

// 重新连接
const redoLink = () => {
  connectStatus.value = 0;
  ws.connect();
};

onMounted(() => {
  try {
    // 初始化webSocketUrl
    ws.setWsUrl();
    ws.connect();
  } catch (error) {
    console.log('error ===>ssss ', error);
  }
});
</script>

<style lang="less">
.wsContainerBox {
  width: 240px;
  background: #ffffff;
  box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.1);
  border: 1px solid #eeeeee;
  padding: 20px;
  box-sizing: border-box;
  border-radius: 10px;
  right: 0;
  position: absolute;
  right: 0;
  bottom: 0;

  .title {
    font-family: PingFangSC, PingFang SC;
    font-weight: 600;
    font-size: 16px;
    color: #000000;
    line-height: 20px;
    text-align: center;

    .close-style {
      width: 1em;
      position: absolute;
      top: 20px;
      right: 20px;
      cursor: pointer;
    }
  }

  .connectStatus {
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;

    img {
      width: 100px;
    }
  }

  .footerBox {
    display: flex;
    flex-direction: column;
    align-items: center;
    // border: 1px solid red;

    .equre {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 16px;
      color: #2f3036;
      font-style: normal;
    }

    .connectStatusDot {
      font-family: PingFangSC, PingFang SC;
      font-size: 14px;
      color: #6a6a6a;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 10px;

      span {
        width: 6px;
        height: 6px;
        background: #00d68a;
        border-radius: 50%;
        margin-right: 5px;
      }
    }

    .closeConnect {
      width: 100px;
      height: 32px;
      background: #ffffff;
      border-radius: 20px;
      border: 1px solid #ff8500;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #ff8500;
    }
  }

  .subTitle {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 12px;
    color: #6a6a6a;
    text-align: center;

    span {
      color: #ff8500;
    }
  }
}
</style>
